//app.js
App({
  onLaunch: function () {
    
  },
  // 去登陆方法
  doLogin(){
    let current = getCurrentPages()[getCurrentPages().length-1].route;
    if(current !== "pages/login/login"){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  // 判断是否登录
  checkLogin(){
    return !!wx.getStorageSync('session_key');
  },
  request(params = {}){ // 封装请求函数
    let baseUrl = "http://localhost:3000";
    let session_key = wx.getStorageSync('session_key');
    return new Promise((resolve,reject)=>{
      wx.showLoading({
        title: '...',
        mask:true
      })
      wx.request({
        url:baseUrl+params.url,
        method:params.method?params.method:'GET',
        header:{
          "session_key":session_key?session_key:''
        },
        data:params.data?params.data:'',
        success:function(res){
          // 判断登录是否过期
          if(res.code === 401 ){
            // wx.removeStorageSync('session_key')
            this.doLogin();
          }
          wx.hideLoading();
          resolve(res);
        },
        fail:function(err){
          reject(err)
        }
      })
    })
  },
  globalData: {
    userInfo: null
  }
})